const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cursor:true,
    focus:false,
    opacity:1,
    shopname:'',
    limit:20,
    page:1,
    maskLayer: true,
    details:{}
  },
  onLoad: function (options){
    if (options.inn_id){
      this.setData({
        inn_id: options.inn_id
      })
    }
  },
  onShow: function () {
    this.storeDetail();
    this.setData({
      shore_blank: app.globalData.base_url +'images/shore_blank.png'
    })
  },
  modify:function(){
    this.setData({
      cursor:false,
      focus:true,
      opacity:0
    })
  },
  lostFocus:function(e){
    var shopname = e.detail.value
    var that =this;
    if (shopname){
      this.setData({
        cursor: true,
        focus: false,
        opacity: 1,
        shopname: e.detail.value
      })
    }else{
      this.setData({
        cursor: true,
        focus: false,
        opacity: 1
      })
    }
    var params = {
      inn_name: this.data.shopname
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + "inn/saveName",
      success: function (res) {
        if (util.isEmpty(res.data.code)) {

        } else {
          that.storeDetail();
          util.showError(res.data.message);
        }
      }
    })


    
  },
  storeDetail:function(){
    var that = this;
    var page = this.data.page;
    var params = {
      inn_id: this.data.inn_id,
      limit: this.data.limit,
      page: this.data.page
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + "inn/getGoodsList",
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          var paging = res.data.data.search;
          var totalPage = paging.total_page;
          var resultList = res.data.data.goods_list;
          var inn_name = res.data.data.inn_info.inn_name;
          var avatar = res.data.data.inn_info.avatar;
          var nickname = res.data.data.inn_info.nickname

          if (page == 1) {
            var itemData = resultList;
          } else {
            var itemData = that.data.product.concat(resultList)
          }

          that.setData({
            total_page: totalPage,
            product: itemData,
            shopname: inn_name,
            avatar: avatar,
            nickname: nickname
          })

        } else {
          util.showError(res.data.message);
        }
      }
    })
  },
  //加载更多
  loadMore: function () {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      page: this.data.page + 1
    });
    this.getProductLIst();
  },
  //页面上拉触底
  onReachBottom: function (e) {
    this.loadMore();
  },
  //商品管理
  management:function(){
    wx.navigateTo({
      url: '../addGoods/addGoods',
    })
  },
  //编辑价格
  editorPrice:function(e){
    var index = e.currentTarget.dataset.index;
    var data = this.data.product[index];
    // console.log(this.data.product);
    this.setData({
      details:data,
      maskLayer:false,
      newPrice:''
    })
  },
  closeModifyPrice:function(){
    this.setData({
      maskLayer: true
    })
  },
  newPrice:function(e){
    var price = e.detail.value;
    this.setData({
      newPrice: price
    })
  },
  changePrice:function(){
    var that= this;
    var bottom_price = this.data.details.bottom_price;
    var newPrice= this.data.newPrice;
    var sku_code = this.data.details.sku_code;
    var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if (reg.test(newPrice)) {
      if (newPrice/1 < bottom_price/1){
        util.showError("价格不得低于最低价");
      }else{
        api.POST({
          params: { sku_code: sku_code, price: newPrice},
          url: app.globalData.base_url + "inn/savePrice",
          success: function (res) {
            if (util.isEmpty(res.data.code)) {
              util.showError("修改成功");
              that.setData({
                page:1
              })
              that.storeDetail();

            } else {
              util.showError(res.data.message);
            }
          }
        })
      }
    }else{
      util.showError("请输入有效价格");
    }
  },
  onShareAppMessage:function(res){
    // console.log(e);
    var that = this;
    var inn_id = this.data.inn_id;
    var types = res.target.dataset.types;
    if (types == 'product') {
      var sku_code = this.data.details.sku_code;
      var url = '/pages/storeProductDetail/storeProductDetail?sku_code=' + sku_code + '&inn_id=' + inn_id
      var title = this.data.details.gname
      var share_img = this.data.details.share_img
    } else if (types == 'store'){
      var url = '/pages/storeGoodsList/storeGoodsList?inn_id=' + inn_id
      var share_img = app.globalData.base_url + 'images/share_01.jpg'
      var title = this.data.shopname +' 邀您来逛逛'
    }

    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: title,
        path: url,
        imageUrl:share_img,
        success: function (res) {
          // that.closeModifyPrice();
          // console.log("222");
          that.setData({
            maskLayer: true
          })
        },
        fail: function (res) {
          that.setData({
            maskLayer: true
          })
        }
      }
    }

  },

  //查看商品详情
  productDetails: function (e) {
    var sku_code = e.currentTarget.dataset.sku_code;
    var inn_id = this.data.inn_id;
    var curTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    this.setData({
      lastTime: curTime
    })
    if (lastTime > 0) {
      if (curTime - lastTime < 500) {
        return false;
      }
    }
    wx.navigateTo({
      url: '../storeProductDetail/storeProductDetail?sku_code=' + sku_code + '&inn_id=' + inn_id
    })
  },
})